package com.hero.jobweb.action.common;

import static com.hero.jobweb.config.MessagesManger.getSystemMessages;
import org.apache.commons.lang.StringUtils;

import com.hero.jobweb.action.ResultKey;
import com.hero.jobweb.config.ExceptionKey;
import com.hero.jobweb.entity.TbUsers;
import com.hero.jobweb.exception.BaseException;
import com.hero.jobweb.service.ICommonService;
import com.hero.web.action.BaseAction;
import com.hero.jobweb.config.SystemKey;

public class LoginAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ICommonService commonService=(ICommonService) getBean("commonServiceImpl");
	private TbUsers tbUser=new TbUsers();

	public TbUsers getTbUser() {
		return tbUser;
	}

	public void setTbUser(TbUsers tbUser) {
		this.tbUser = tbUser;
	}

	/***
	 * 用户登录
	 */
	public String userLogin()
	{
		try
		{
			TbUsers loginUser= commonService.userLogin(tbUser);
			String userType=loginUser.getUserTypeCode();//用户类型
			if(StringUtils.equals(userType, getSystemMessages(SystemKey.DEFAULT_ENTERPRISE_USER_TYPE_CODE)))//企业用户
			{
				session.setAttribute(SystemKey.ENTERPRISE_ID.toString(), loginUser.getDepartment().getEnterpriseId());
				session.setAttribute(SystemKey.ENTERPRISE_USER.toString(), loginUser);
				session.setAttribute(SystemKey.ENTERPRISE_USER_ID.toString(), loginUser.getId());
				return ResultKey.ENTERPRISE_INDEX.toString();
			}
			if(StringUtils.equals(userType, getSystemMessages(SystemKey.DEFAULT_PERSONAL_USER_TYPE_CODE)))//个人用户
			{
				session.setAttribute(SystemKey.PERSONAL_USER.toString(), loginUser);
				session.setAttribute(SystemKey.PERSONAL_USER_ID.toString(), loginUser.getId());
				return ResultKey.PERSONAL_INDEX.toString();
			}
			
		}catch(BaseException be)
		{
			addFieldError(ExceptionKey.USER_LOGIN_ERROR.toString(),be.getMessage());
			return INPUT;
		}		
		return ResultKey.JOB_WEB_INDEX.toString();
	}
	
	/**
	 * 个人用户退出登陆
	 * @return
	 */
	public String personalLogout()
	{
		session.removeAttribute(SystemKey.PERSONAL_USER.toString());
		session.removeAttribute(SystemKey.PERSONAL_USER_ID.toString());
		return ResultKey.JOB_WEB_INDEX.toString();
	}
	
	
	/**
	 * 企业用户退出登陆
	 * @return
	 */
	public String enterpriseLogout()
	{
		session.removeAttribute(SystemKey.ENTERPRISE_USER.toString());
		session.removeAttribute(SystemKey.ENTERPRISE_USER_ID.toString());
		session.removeAttribute(SystemKey.ENTERPRISE_ID.toString());
		return ResultKey.JOB_WEB_INDEX.toString();
	}
}
